#include "count_down_latch.h"
#include "logger.h"

namespace mars {

CountDownLatch::CountDownLatch(int count):
     _mutex(),
    _cond(),
    _count(count)    {
}

void CountDownLatch::wait() {
    MutexLockGuard guard(_mutex);
    while(_count > 0) {
        _cond.wait(_mutex);
    }
    DEBUG_PRINT("wait finished. count:%d\n", _count);
}

void CountDownLatch::count_down() {
    MutexLockGuard guard(_mutex);
    --_count;
    DEBUG_PRINT("count:%d\n", _count);
    if (_count == 0) {
        _cond.notifyAll();
    }
}


int CountDownLatch::get_count() {
    MutexLockGuard guard(_mutex);
    return _count;
}

} // namespace mars
